function [fitresult, gof] = createFits(month, pressure, t, ppg_data)
%CREATEFITS(MONTH,PRESSURE,T,PPG_DATA)
%  Create fits.
%
%  Data for 'Fourier' fit:
%      X Input : month
%      Y Output: pressure
%  Data for 'Enso1Period' fit:
%      X Input : month
%      Y Output: pressure
%  Data for 'Enso2Period' fit:
%      X Input : month
%      Y Output: pressure
%  Data for 'Enso3Period' fit:
%      X Input : month
%      Y Output: pressure
%  Data for 'PPG' fit:
%      X Input : t
%      Y Output: ppg_data
%  Output:
%      fitresult : a cell-array of fit objects representing the fits.
%      gof : structure array with goodness-of fit info.
%
%  See also FIT, CFIT, SFIT.

%  Auto-generated by MATLAB on 18-Mar-2016 17:05:40

%% Initialization.

% Initialize arrays to store fits and goodness-of-fit.
fitresult = cell( 5, 1 );
gof = struct( 'sse', cell( 5, 1 ), ...
    'rsquare', [], 'dfe', [], 'adjrsquare', [], 'rmse', [] );

%% Fit: 'Fourier'.
[xData, yData] = prepareCurveData( month, pressure );

% Set up fittype and options.
ft = fittype( 'fourier8' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
opts.StartPoint = [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0752477282296956];

% Fit model to data.
[fitresult{1}, gof(1)] = fit( xData, yData, ft, opts );

% Plot fit with data.
figure( 'Name', 'Fourier' );
h = plot( fitresult{1}, xData, yData );
legend( h, 'pressure vs. month', 'Fourier', 'Location', 'NorthEast' );
% Label axes
xlabel( 'month' );
ylabel( 'pressure' );
grid on

%% Fit: 'Enso1Period'.
[xData, yData] = prepareCurveData( month, pressure );

% Set up fittype and options.
ft = fittype( 'a0+a1*cos(2*pi*x/c1)+b1*sin(2*pi*x/c1)', 'independent', 'x', 'dependent', 'y' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
opts.Lower = [-Inf -Inf -Inf 10];
opts.StartPoint = [0.0181775336366957 0.683838613746355 0.783736480083219 0.534137567882728];
opts.Upper = [Inf Inf Inf 14];

% Fit model to data.
[fitresult{2}, gof(2)] = fit( xData, yData, ft, opts );

% Create a figure for the plots.
figure( 'Name', 'Enso1Period' );

% Plot fit with data.
subplot( 2, 1, 1 );
h = plot( fitresult{2}, xData, yData );
legend( h, 'pressure vs. month', 'Enso1Period', 'Location', 'NorthEast' );
% Label axes
xlabel( 'month' );
ylabel( 'pressure' );
grid on

% Plot residuals.
subplot( 2, 1, 2 );
h = plot( fitresult{2}, xData, yData, 'residuals' );
legend( h, 'Enso1Period - residuals', 'Zero Line', 'Location', 'NorthEast' );
% Label axes
xlabel( 'month' );
ylabel( 'pressure' );
grid on

%% Fit: 'Enso2Period'.
[xData, yData] = prepareCurveData( month, pressure );

% Set up fittype and options.
ft = fittype( 'a0+a1*cos(2*pi*x/c1)+b1*sin(2*pi*x/c1)+a2*cos(2*pi*x/c2)+b2*sin(2*pi*x/c2)', 'independent', 'x', 'dependent', 'y' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
opts.Lower = [-Inf -Inf -Inf -Inf -Inf -Inf 10];
opts.StartPoint = [0.137868992412558 0.217801593712125 0.106941658550207 0.182141075890434 0.616443485085685 0.0418198639729543 0.939661010161067];
opts.Upper = [Inf Inf Inf Inf Inf Inf 14];

% Fit model to data.
[fitresult{3}, gof(3)] = fit( xData, yData, ft, opts );

% Create a figure for the plots.
figure( 'Name', 'Enso2Period' );

% Plot fit with data.
subplot( 2, 1, 1 );
h = plot( fitresult{3}, xData, yData );
legend( h, 'pressure vs. month', 'Enso2Period', 'Location', 'NorthEast' );
% Label axes
xlabel( 'month' );
ylabel( 'pressure' );
grid on

% Plot residuals.
subplot( 2, 1, 2 );
h = plot( fitresult{3}, xData, yData, 'residuals' );
legend( h, 'Enso2Period - residuals', 'Zero Line', 'Location', 'NorthEast' );
% Label axes
xlabel( 'month' );
ylabel( 'pressure' );
grid on

%% Fit: 'Enso3Period'.
[xData, yData] = prepareCurveData( month, pressure );

% Set up fittype and options.
ft = fittype( 'a0+a1*cos(2*pi*x/c1)+b1*sin(2*pi*x/c1)+a2*cos(2*pi*x/c2)+b2*sin(2*pi*x/c2)+a3*cos(2*pi*x/c3)+b3*sin(2*pi*x/c3)', 'independent', 'x', 'dependent', 'y' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
opts.Lower = [-Inf -Inf -Inf -Inf -Inf -Inf -Inf 10 20 36];
opts.StartPoint = [0.137868992412558 0.217801593712125 0.106941658550207 0.7668 0.182141075890434 0.616443485085685 0.336699264391476 0.0418198639729543 0.939661010161067 0.662381860399481];
opts.Upper = [Inf Inf Inf Inf Inf Inf Inf 14 30 Inf];

% Fit model to data.
[fitresult{4}, gof(4)] = fit( xData, yData, ft, opts );

% Create a figure for the plots.
figure( 'Name', 'Enso3Period' );

% Plot fit with data.
subplot( 2, 1, 1 );
h = plot( fitresult{4}, xData, yData );
legend( h, 'pressure vs. month', 'Enso3Period', 'Location', 'NorthEast' );
% Label axes
xlabel( 'month' );
ylabel( 'pressure' );
grid on

% Plot residuals.
subplot( 2, 1, 2 );
h = plot( fitresult{4}, xData, yData, 'residuals' );
legend( h, 'Enso3Period - residuals', 'Zero Line', 'Location', 'NorthEast' );
% Label axes
xlabel( 'month' );
ylabel( 'pressure' );
grid on

%% Fit: 'PPG'.
[xData, yData] = prepareCurveData( t, ppg_data );

% Set up fittype and options.
ft = fittype( 'fourier8' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
opts.StartPoint = [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.104737211321547];

% Fit model to data.
[fitresult{5}, gof(5)] = fit( xData, yData, ft, opts );

% Plot fit with data.
figure( 'Name', 'PPG' );
h = plot( fitresult{5}, xData, yData );
legend( h, 'ppg_data vs. t', 'PPG', 'Location', 'NorthEast' );
% Label axes
xlabel( 't' );
ylabel( 'ppg_data' );
grid on


